<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Minim : : Sampler</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="stylesheet.css" rel="stylesheet" type="text/css">
</head>
<body> 
<center>
<table class="mainTable">
  <tr>
    <td class="header">
    	<span class="indexheader">Minim</span><br/>
    	<span class="indexnavigation">
    		<a href="index.html">core</a> | 
    		<a href="index_ugens.html">ugens</a> | 
    		<a href="index_analysis.html">analysis</a>
    	</span>
    </td>
    <td class="border-left">&nbsp;</td>
  </tr>
  
  <tr>
    <td class="classNavigation">
    	<p class="mainTextName">Sampler</p>
    	
       <p class="linkListHeader">Fields</p>
       <p class="linkList">
           
    <a href="sampler_field_amplitude.html" title="The amplitude of this Sampler. This acts as an
 overall volume control. So changing the amplitude
 will effect all currently active voices.">amplitude</a><br/>
    
    <a href="sampler_field_attack.html" title="The attack time, in seconds, when triggering 
 this Sampler. Attack time is used to ramp up 
 the amplitude of the voice. By default it 
 is 0 seconds.">attack</a><br/>
    
    <a href="sampler_field_begin.html" title="The sample number in the source sample 
 the voice will start at when triggering this Sampler.">begin</a><br/>
    
    <a href="sampler_field_end.html" title="The sample number in the source sample 
 the voice will end at when triggering this Sampler.">end</a><br/>
    
    <a href="sampler_field_looping.html" title="Whether triggered voices should loop or not.">looping</a><br/>
    
    <a href="sampler_field_rate.html" title="The playback rate used when triggering this Sampler.">rate</a><br/>
    
	   </p>
   
    	
       <p class="linkListHeader">Methods</p>
       <p class="linkList">
           
    <a href="sampler_method_channelcount.html" title="Returns the number of channels this UGen has been configured to generate.">channelCount ( )</a><br/>
    
    <a href="sampler_method_getlastvalues.html" title="Return the last values generated by this UGen. This will most often be
 used by sub-classes when pulling data from their inputs.">getLastValues ( )</a><br/>
    
    <a href="sampler_method_patch.html" title="Send the output of this UGen to another UGen, UGenInput, or AudioOutput.">patch ( )</a><br/>
    
    <a href="sampler_method_printinputs.html" title="Prints all inputs connected to this UGen (for debugging)">printInputs ( )</a><br/>
    
    <a href="sampler_method_samplerate.html" title="Returns the sample rate of this UGen.">sampleRate ( )</a><br/>
    
    <a href="sampler_method_setchannelcount.html" title="Let this UGen know how many channels of audio you will be asking it for.">setChannelCount ( )</a><br/>
    
    <a href="sampler_method_setsample.html" title="Sets the sample data used by this Sampler by <em>copying</em> the 
 contents of the provided MultiChannelBuffer into the internal buffer.">setSample ( )</a><br/>
    
    <a href="sampler_method_setsamplerate.html" title="Set the sample rate for this UGen.">setSampleRate ( )</a><br/>
    
    <a href="sampler_method_stop.html" title="Stop all active voices. In other words,
 immediately silence this Sampler.">stop ( )</a><br/>
    
    <a href="sampler_method_tick.html" title="Generates one sample frame for this UGen.">tick ( )</a><br/>
    
    <a href="sampler_method_trigger.html" title="Trigger this Sampler.">trigger ( )</a><br/>
    
    <a href="sampler_method_unpatch.html" title="Unpatch this UGen from an AudioOutput or other UGen.">unpatch ( )</a><br/>
    
	   </p>
   
    </td>
    <td class="mainText border-left">
    	Sampler is the UGen version of AudioSample and is
 the preferred method of triggering short audio files. 
 You will also find Sampler much more flexible,
 since it provides ways to trigger only part of a sample, and 
 to trigger a sample at different playback rates. Also, unlike AudioSample,
 a Sampler lets you specify how many voices (i.e. simultaneous 
 playbacks of the sample) should have.
 <p>
 Sampler provides several inputs that allow you to control the properties
 of a triggered sample. When you call the trigger method, the values of these 
 inputs are "snapshotted" and used to configure the new voice that will play 
 the sample. So, changing the values does not effect already playing voices,
 except for <code>amplitude</code>, which controls the volume of the Sampler 
 as a whole.
    	<p class="memberSectionHeader">Constructors</p>
    	<pre><em>Create a new Sampler for triggering the provided file.</em>
Sampler(String filename, int maxVoices, Minim system)
<em>Create a Sampler that will use the audio in the provided MultiChannelBuffer
 for its sample. It will make a copy of the data, so modifying the provided
 buffer after the fact will not change the audio in this Sampler.
 The original sample rate of the audio data must be provided
 so that the default playback rate of the Sampler can be set properly.
 Additionally, you must specify how many voices the Sampler should use,
 which will determine how many times the sound can overlap with itself
 when triggered.</em>
Sampler(MultiChannelBuffer sampleData, float sampleRate, int maxVoices)
</pre>
    	
   <p class="memberSectionHeader">Parameters</p>
   
        <span class="parameterName">filename</span>&nbsp;&mdash;&nbsp;<span class="parameterDescription">String: the file to load</span><br/>
    
        <span class="parameterName">maxVoices</span>&nbsp;&mdash;&nbsp;<span class="parameterDescription">int: the maximum number of voices for this Sampler</span><br/>
    
        <span class="parameterName">system</span>&nbsp;&mdash;&nbsp;<span class="parameterDescription">Minim: the instance of Minim to use for loading the file</span><br/>
    
        <span class="parameterName">sampleData</span>&nbsp;&mdash;&nbsp;<span class="parameterDescription">MultiChannelBuffer: the sample data this Sampler will use to generate sound</span><br/>
    
        <span class="parameterName">sampleRate</span>&nbsp;&mdash;&nbsp;<span class="parameterDescription">float: the sample rate of the sampleData</span><br/>
    
   
    	<p class="memberSectionHeader">Related</p>
    	<A href="audiosample_class_audiosample.html">AudioSample</A><BR>
<A href="ugen_class_ugen.html">UGen</A><BR>

    	<p class="memberSectionHeader">Example</p>
    	<pre>import processing.opengl.*;

/**
  * This sketch is a more involved use of AudioSamples to create a simple drum machine. 
  * Click on the buttons to toggle them on and off. The buttons that are on will trigger 
  * samples when the beat marker passes over their column. You can change the tempo by 
  * clicking in the BPM box and dragging the mouse up and down.
  * &lt;p>
  * We achieve the timing by using AudioOutput's playNote method and a cleverly written Instrument.
  * &lt;p>
  * For more information about Minim and additional features, 
  * visit http://code.compartmental.net/minim/
  */


import ddf.minim.*;
import ddf.minim.ugens.*;

Minim       minim;
AudioOutput out;

Sampler     kick;
Sampler     snare;
Sampler     hat;

boolean[] hatRow = new boolean[16];
boolean[] snrRow = new boolean[16];
boolean[] kikRow = new boolean[16];

ArrayList&lt;Rect> buttons = new ArrayList&lt;Rect>();

int bpm = 120;

int beat; // which beat we're on

// here's an Instrument implementation that we use 
// to trigger Samplers every sixteenth note. 
// Notice how we get away with using only one instance
// of this class to have endless beat making by 
// having the class schedule itself to be played
// at the end of its noteOff method. 
class Tick implements Instrument
{
  void noteOn( float dur )
  {
    if ( hatRow[beat] ) hat.trigger();
    if ( snrRow[beat] ) snare.trigger();
    if ( kikRow[beat] ) kick.trigger();
  }
  
  void noteOff()
  {
    // next beat
    beat = (beat+1)%16;
    // set the new tempo
    out.setTempo( bpm );
    // play this again right now, with a sixteenth note duration
    out.playNote( 0, 0.25f, this );
  }
}

// simple class for drawing the gui
class Rect 
{
  int x, y, w, h;
  boolean[] steps;
  int stepId;
  
  public Rect(int _x, int _y, boolean[] _steps, int _id)
  {
    x = _x;
    y = _y;
    w = 14;
    h = 30;
    steps = _steps;
    stepId = _id;
  }
  
  public void draw()
  {
    if ( steps[stepId] )
    {
      fill(0,255,0);
    }
    else
    {
      fill(255,0,0);
    }
    
    rect(x,y,w,h);
  }
  
  public void mousePressed()
  {
    if ( mouseX >= x && mouseX &lt;= x+w && mouseY >= y && mouseY &lt;= y+h )
    {
      steps[stepId] = !steps[stepId];
    }
  }
}

void setup()
{
  size(395, 200);
  minim = new Minim(this);
  out   = minim.getLineOut();
  
  // load all of our samples, using 4 voices for each.
  // this will help ensure we have enough voices to handle even
  // very fast tempos.
  kick  = new Sampler( "BD.wav", 4, minim );
  snare = new Sampler( "SD.wav", 4, minim );
  hat   = new Sampler( "CHH.wav", 4, minim );
  
  // patch samplers to the output
  kick.patch( out );
  snare.patch( out );
  hat.patch( out );
  
  for (int i = 0; i &lt; 16; i++)
  {
    buttons.add( new Rect(10+i*24, 50, hatRow, i ) );
    buttons.add( new Rect(10+i*24, 100, snrRow, i ) );
    buttons.add( new Rect(10+i*24, 150, kikRow, i ) );
  }
  
  beat = 0;
  
  // start the sequencer
  out.setTempo( bpm );
  out.playNote( 0, 0.25f, new Tick() );
}

void draw()
{
  background(0);
  fill(255);
  //text(frameRate, width - 60, 20);
  
  for(int i = 0; i &lt; buttons.size(); ++i)
  {
    buttons.get(i).draw();
  }
  
  stroke(128);
  if ( beat % 4 == 0 )
  {
    fill(200, 0, 0);
  }
  else
  {
    fill(0, 200, 0);
  }
    
  // beat marker    
  rect(10+beat*24, 35, 14, 9);
}

void mousePressed()
{
  for(int i = 0; i &lt; buttons.size(); ++i)
  {
    buttons.get(i).mousePressed();
  }
}</pre>
    	<p class="memberSectionHeader">Usage</p>
    	Web & Application
    </td>
  </tr>
</table>
</center>
</body>
</html>